package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.Address;
import com.cskaoyan.bean.pojo.AddressExample;
import com.cskaoyan.bean.pojo.Region;
import com.cskaoyan.bean.wxbo.WxAddressSaveBO;
import com.cskaoyan.bean.wxvo.AddressDetailVO;
import com.cskaoyan.bean.wxvo.AddressList;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Author Gan
 * @Date 2021/7/12 14:32
 * @Description:
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    RegionMapper regionMapper;

    @Autowired
    AddressMapper addressMapper;

    @Override
    public String selectAddressByUserId(Integer userId) {

        return regionMapper.queryAddressById(userId);
    }

    @Override
    public int selectIsDefaultById(Integer userId) {
        return regionMapper.queryIsDefaultById(userId);
    }

    @Override
    public List<Integer> selectIdByUserId(Integer userId) {
        return regionMapper.queryIdByUserId(userId);
    }

    @Override
    public String selectMobileById(Integer userId) {
        return regionMapper.queryMobileById(userId);
    }

    @Override
    public String selectNameById(Integer userId) {
        return regionMapper.queryNameById(userId);
    }

    @Override
    public int selectAreaIdById(Integer userId) {
        return regionMapper.queryAreaIdById(userId);
    }

    @Override
    public String selectAreaNameByAreaId(Integer areaId) {
        return regionMapper.queryAreaNameByAreaId(areaId);
    }

    @Override
    public int selectCityIdById(Integer userId) {
        return regionMapper.queryCityIdById(userId);
    }

    @Override
    public String selectCityNameByCityId(int cityId) {
        return regionMapper.queryCityNameByCityId(cityId);
    }

    @Override
    public int selectProvinceIdById(Integer userId) {
        return regionMapper.queryProvinceIdById(userId);
    }

    @Override
    public String selectProvinceNameByProvinceId(int provinceId) {
        return regionMapper.queryProvinceNameByProvinceId(provinceId);
    }

    @Override
    public String selectAddressById2(Integer userId) {
        return regionMapper.selectAddressById2(userId);
    }

    @Override
    public List<AddressList> addressList(Integer userId) {

        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        //未删除
        criteria.andDeletedEqualTo(false);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        List<AddressList> addressLists = new ArrayList<>();
        Iterator<Address> iterator = addresses.iterator();
        while (iterator.hasNext()) {
            Address address = iterator.next();
            AddressList addressList = new AddressList();
            Region province = regionMapper.selectByPrimaryKey(address.getProvinceId());
            Region area = regionMapper.selectByPrimaryKey(address.getAreaId());
            Region city = regionMapper.selectByPrimaryKey(address.getCityId());
            String addressPro = province.getName() + city.getName() + area.getName();

            addressList.setIsDefault(address.getIsDefault());
            addressList.setDetailedAddress(addressPro + " " + address.getAddress());
            addressList.setId(address.getId());
            addressList.setMobile(address.getMobile());
            addressList.setName(address.getName());
            addressLists.add(addressList);
        }
        return addressLists;
    }

    @Override
    public int setDeleteStatus(Integer id) {
        return regionMapper.setDeleteStatus(id);
    }

    @Override
    public int addAddress(WxAddressSaveBO wxAddressSaveBO, Integer userId) {
        //插入数据库中使用
        Address address = new Address();

        //第一次创建地址，就设置为默认地址
        if (wxAddressSaveBO.getIsDefault()) {
            //把其他的改成false
            Address defaultAddress = new Address();
            defaultAddress.setIsDefault(false);

            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();

            criteria.andUserIdEqualTo(userId);
            criteria.andIsDefaultEqualTo(true);
            //
            addressMapper.updateByExampleSelective(defaultAddress, addressExample);
        }
        address.setAddress(wxAddressSaveBO.getAddress());
        address.setAreaId(wxAddressSaveBO.getAreaId());
        address.setCityId(wxAddressSaveBO.getCityId());
        address.setId(wxAddressSaveBO.getId());
        address.setIsDefault(wxAddressSaveBO.getIsDefault());
        address.setMobile(wxAddressSaveBO.getMobile());
        address.setName(wxAddressSaveBO.getName());
        address.setProvinceId(wxAddressSaveBO.getProvinceId());
        address.setUserId(userId);
        addressMapper.insertSelective(address);
        //主键回写
        return address.getId();
    }

    @Override
    public void updateAddress(WxAddressSaveBO wxAddressSaveBO, Integer userId) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIdEqualTo(wxAddressSaveBO.getId());
        Address address = new Address();
        address.setAddress(wxAddressSaveBO.getAddress());
        address.setAreaId(wxAddressSaveBO.getAreaId());
        address.setCityId(wxAddressSaveBO.getCityId());
        address.setIsDefault(wxAddressSaveBO.getIsDefault());
        address.setId(wxAddressSaveBO.getId());
        address.setMobile(wxAddressSaveBO.getMobile());
        address.setName(wxAddressSaveBO.getName());
        address.setProvinceId(wxAddressSaveBO.getProvinceId());
        //若设置为默认地址
        if (wxAddressSaveBO.getIsDefault()) {
            //把其他的改成false
            Address defaultAddress = new Address();
            defaultAddress.setIsDefault(false);

            AddressExample addressExample2 = new AddressExample();
            AddressExample.Criteria criteria2 = addressExample2.createCriteria();

            criteria2.andIsDefaultEqualTo(true);
            criteria2.andUserIdEqualTo(userId);
            //
            addressMapper.updateByExampleSelective(defaultAddress, addressExample2);
        }
        //只是修改其他信息，没有保存为默认地址
        addressMapper.updateByExampleSelective(address, addressExample);
    }

    @Override
    public Integer saveAddress(WxAddressSaveBO wxAddressSaveBO, Integer userId) {
        Date addTime;
        if (wxAddressSaveBO.getId() != 0) {
            addTime = addressMapper.selectByPrimaryKey(wxAddressSaveBO.getId()).getAddTime();
        }
        addTime = new Date();
        Address address1 = new Address(wxAddressSaveBO.getId() == 0 ? null : wxAddressSaveBO.getId(),
                wxAddressSaveBO.getName(), userId, wxAddressSaveBO.getProvinceId(), wxAddressSaveBO.getCityId(),
                wxAddressSaveBO.getAreaId(), wxAddressSaveBO.getAddress(), wxAddressSaveBO.getMobile(),
                wxAddressSaveBO.getIsDefault(), addTime, new Date(), false);

        if (wxAddressSaveBO.getId() != 0) {
            //修改信息字段
            addressMapper.updateByPrimaryKeySelective(address1);
        } else addressMapper.insertSelective(address1);

        if (wxAddressSaveBO.getId() != 0) {
            //修改信息字段
            addressMapper.updateByPrimaryKeySelective(address1);
        } else addressMapper.insertSelective(address1);

        if (wxAddressSaveBO.getIsDefault()) {
            //当前ID的地址设置为默认地址
            int id = wxAddressSaveBO.getId();//地址id
            Address defaultAddress = new Address();
            defaultAddress.setId(id);
            defaultAddress.setIsDefault(true);
            int i = addressMapper.updateByPrimaryKeySelective(defaultAddress);

            //把用户的其他地址改成false
            AddressExample addressExample = new AddressExample();
            //判断条件
            addressExample.createCriteria()
                    .andUserIdEqualTo(userId)   //当前用户
                    .andDeletedEqualTo(false)// 有效地址
                    .andIsDefaultEqualTo(true)// 被设为默认
                    .andIdNotEqualTo(id);  //同时不是刚刚修改为默认的地址
            Address oldDefaultAddress = new Address();
            oldDefaultAddress.setIsDefault(false);
            int i1 = addressMapper.updateByExampleSelective(oldDefaultAddress, addressExample);
        }

        return wxAddressSaveBO.getId();

    }

}
